package com.educate.controller;

import com.educate.pojo.Note;
import com.educate.service.NoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author xiongyu
 * @date 2021/10/28 20:59
 */

@RestController
@RequestMapping("/note")
@Slf4j
public class NoteController {

    @Autowired
    NoteService noteService;

    /**
     * 增加图书
     * @param note
     * @return
     */
    @PostMapping("/add")
    public long addNote(@RequestBody Note note){
        noteService.addNote(note);
        return note.getNoteId();
    }

    /**
     * 删除图书
     * @param noteId
     * @return
     */
    @PostMapping("/delete")
    public boolean deleteNote(@RequestParam Integer noteId){
        System.out.println(noteId);
        return noteService.deleteNote(noteId);
    }

    /**
     * 跟新图书
     * @param note
     * @return
     */

    @PostMapping("/update")
    public boolean updateNote(@RequestBody Note note){
        return noteService.updateNote(note);
    }

    /**
     * 查询图书
     * @param noteId
     * @return
     */
    @GetMapping("/queryNote")
    public Note queryNotes(@RequestParam int noteId){
        return noteService.queryNote(noteId);
    }

    /**
     * elasticsearch模糊查询
     * @param title
     * @return
     */
    @GetMapping("/queryNotesByTitle")
    public Map<String,Object> queryNotesByTitle(@RequestParam String title,@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return noteService.queryNoteByTitle(title,pageNum,pageSize);
    }

    /**
     * 根据作者id分页查询笔记
     * @param authorId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/queryAllNotes")
    Map queryAllNotes(@RequestParam Long authorId,@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        return  noteService.queryAllNotes(authorId,pageNum,pageSize);
    }

}
